package com.woniu.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @Classname MyConfig
 * @Description TODO
 * @Date 2025/7/22 14:15
 * @Created by pc
 */
@Configuration
public class MyConfig {

    // 异步生成订单
    @Bean
    public Queue orderQueue(){
        return new Queue("orderQueue");
    }
    // 生成订单的交换机
    @Bean
    public TopicExchange orderExchange(){
        return new TopicExchange("orderExchange");
    }

    @Bean
    public Binding orderQueueToExchange(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with("order");
    }

    // 延迟消费队列
    @Bean
    public Queue checkQueue(){
        return new Queue("checkQueue");
    }
    // idea自定义交换机
    @Bean
    public CustomExchange checkExchange(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-delayed-type","topic");
        return new CustomExchange("checkExchange",
                "x-delayed-message",
                true,false,map);
    }
    @Bean
    public Binding checkQueueToExchange(){
        return BindingBuilder.bind(checkQueue()).to(checkExchange()).with("check").noargs();
    }

    // 重新定义rabbitMQ的序列化方式
    @Bean
    public MessageConverter Jackson2JsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

}
